//
//  WoYaoJuBaoTableViewController.swift
//  xdjcy
//
//  Created by Henry on 15/12/8.
//  Copyright (c) 2015年 Henry. All rights reserved.
//

import UIKit

class WoYaoJuBaoTableViewController: UITableViewController,UIGestureRecognizerDelegate,UITextViewDelegate {
    
    @IBOutlet weak var txt_reportedContent: UITextView!
    @IBOutlet weak var txt_reportName: XHTextField!
    

    @IBOutlet weak var txt_reportPhone: XHTextField!
    
    @IBOutlet weak var txt_reportCard: XHTextField!
    
    @IBOutlet weak var txt_reportedName: XHTextField!
    
    @IBOutlet weak var txt_reportedCompany: XHTextField!
    
    @IBOutlet weak var txt_reportedArea: XHTextField!
    
    
    
    
    @IBOutlet weak var txt_codeName: GSIconPickerTextField!
    
    @IBOutlet weak var txt_zhiwei: GSIconPickerTextField!
    
    @IBOutlet weak var txt_shenfen: GSIconPickerTextField!
    
    @IBOutlet weak var txt_zuiming: GSIconPickerTextField!
    var progress:MBProgressHUD!
    
    var cardList:Array<TypeModel> = Array<TypeModel>()
    var positionList:Array<TypeModel> = Array<TypeModel>()
    var identityList:Array<TypeModel> = Array<TypeModel>()
    var crimesList:Array<TypeModel> = Array<TypeModel>()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.interactivePopGestureRecognizer.delegate = self
        
        self.navigationController?.navigationBar.tintColor =  UIColor.whiteColor()
        var backButton = UIBarButtonItem(image: UIImage(named: "back"), style: UIBarButtonItemStyle.Bordered, target: self, action: "goBack")
        self.navigationItem.leftBarButtonItem = backButton
        
        var saveButton = UIBarButtonItem(image: UIImage(named: "save"), style: UIBarButtonItemStyle.Bordered, target: self, action: "goSave")
        self.navigationItem.rightBarButtonItem = saveButton
        
        txt_codeName.layer.borderWidth = 0.8
        txt_codeName.layer.borderColor = UIColor(white: 0.1, alpha: 0.2).CGColor
        
        txt_zhiwei.layer.borderWidth = 0.8
        txt_zhiwei.layer.borderColor = UIColor(white: 0.1, alpha: 0.2).CGColor
        
        txt_shenfen.layer.borderWidth = 0.8
        txt_shenfen.layer.borderColor = UIColor(white: 0.1, alpha: 0.2).CGColor
        
        txt_zuiming.layer.borderWidth = 0.8
        txt_zuiming.layer.borderColor = UIColor(white: 0.1, alpha: 0.2).CGColor
        
        txt_reportedContent.layer.borderWidth = 0.8
        txt_reportedContent.layer.borderColor = UIColor(white: 0.1, alpha: 0.2).CGColor
       
        progress = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
        MenuTypeRN.sharedInstance.getBasicList { (result, message, cardList, jobLevelList, identityList, crimesList) -> Void in
            self.progress.hide(true)
            if result
            {
                self.cardList = cardList
                self.positionList = jobLevelList
                self.identityList = identityList
                self.crimesList = crimesList
                self.txt_codeName.list = self.cardList
                self.txt_zhiwei.list = self.positionList
                self.txt_shenfen.list = self.identityList
                self.txt_zuiming.list = self.crimesList
            }
        }
        
        
        txt_reportedContent.autocorrectionType = UITextAutocorrectionType.Yes
        txt_reportedContent.autocapitalizationType = UITextAutocapitalizationType.None
        txt_reportedContent.keyboardType = UIKeyboardType.Default
        txt_reportedContent.returnKeyType = UIReturnKeyType.Default
        txt_reportedContent.delegate = self
        
        var topview = UIToolbar(frame: CGRectMake(0, 0, 320, 44))
        topview.barStyle = UIBarStyle.Default
        
        var previousBarButton = UIBarButtonItem(title: "Previous", style: UIBarButtonItemStyle.Bordered, target: self, action: nil)
        previousBarButton.enabled = false
        var nextBarButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Bordered, target: self, action: nil)
        nextBarButton.enabled = false
        var btnSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
        
        var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "dismissKeyBoard")
        
        var buttonsArray = NSArray(array: [previousBarButton,nextBarButton,btnSpace,doneButton])
        
        topview.setItems(buttonsArray as [AnyObject], animated: true)
        
        txt_reportedContent.inputAccessoryView = topview
        
        txt_reportName.required = true
        txt_reportPhone.required = true
        txt_reportCard.required = true
        txt_reportName.maxTextLength = 10
        txt_reportPhone.maxTextLength = 11
        txt_reportCard.maxTextLength = 18
        
        txt_reportedName.required = true
        txt_reportedCompany.required = true
        txt_reportedArea.required = true
        txt_reportedName.maxTextLength = 10
        txt_reportedCompany.maxTextLength = 30
        txt_reportedArea.maxTextLength = 30
    
    
    }
    
    func dismissKeyBoard()
    {
        self.txt_reportedContent.resignFirstResponder()
    }
    
    func goBack()
    {
        self.navigationController?.popViewControllerAnimated(true)
    }
    
    func goSave()
    {
        
        
        if txt_reportName.validate() && txt_reportPhone.validate() && txt_codeName.text != "" && txt_reportCard.validate() && txt_reportedName.validate() && txt_zhiwei.text != "" && txt_reportedCompany.validate() && txt_reportedArea.validate() && txt_shenfen.text != "" && txt_zuiming.text != "" && txt_reportedContent.text != ""
        {
            var model = JBModel()
            model.Report_name = txt_reportName.text
            model.Report_phone = txt_reportPhone.text
            model.Report_idcardtype = self.cardList[self.txt_codeName.selectedIndex].typeCode
            model.Report_idcard = txt_reportCard.text
            model.Reported_name = txt_reportedName.text
            model.Reported_joblevel = self.positionList[self.txt_zhiwei.selectedIndex].typeCode
            model.Reported_company = txt_reportedCompany.text
            model.Reported_cimearea = txt_reportedArea.text
            model.Reported_idendtity = self.identityList[txt_shenfen.selectedIndex].typeCode
            model.Reported_allegedcrimes = self.crimesList[txt_zuiming.selectedIndex].typeCode
            model.Reported_content = txt_reportedContent.text
            
            progress = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
            MenuTypeRN.sharedInstance.ReportSave(model, did: { (result, message) -> Void in
                self.progress.mode = MBProgressHUDMode.Text
                self.progress.labelText = message
                self.progress.hide(true, afterDelay: 2)
                if result
                {
                    self.goBack()
                }
               
            })
        }
        else
        {
            progress = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
            progress.mode = MBProgressHUDMode.Text
            progress.labelText = "请填写完整信息"
            progress.hide(true, afterDelay: 2)
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40.0
    }
    
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1

    }
    
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerview = UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 40.0))
        var headertext = UILabel(frame: CGRectMake(10, 10, tableView.bounds.size.width - 10, 20))
        headertext.font = UIFont(name: "System", size: 14)
        if section == 0
        {
             headertext.text = "举报人基本情况"
        }
        else
        {
             headertext.text = "被举报人基本情况"
        }
        

       
        headerview.addSubview(headertext)
        return headerview
    }
    

    // MARK: - Table view data source

//    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
//        // #warning Potentially incomplete method implementation.
//        // Return the number of sections.
//        return 0
//    }
//
//    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        // #warning Incomplete method implementation.
//        // Return the number of rows in the section.
//        return 0
//    }

    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}
